mod common;

use common::{dourl, eq_obj_notime};
use serde_json::json;
use std::{file, line};

#[tokio::test]
async fn b_chunk() {
    let data = json!({
        "n": "chunk,chunk2"
    });
    let ret = dourl(&data, "rmtab").await.expect("error_1");
    assert_eq!(ret, json!({"code": 1000, "fail": false, "success": true}));

    let data = json!({
            "v": [{
                "tab": "chunk",
                "line": "line-one",
                "data": [{
                    "psn": "p1001",
                    "wsn": "w001",
                    "k": {"usn": "p 100 1", "esn": "e001"},
                    "v": {"d1":11, "d2":21, "d3":21}
                }, {
                    "psn": "p1002",
                    "wsn": "w001",
                    "k": {"esn": "e001", "usn": "p 100 1"},
                    "v": {"d1":12, "d2":22, "d3":22}
                }, {
                    "psn": "p1003",
                    "wsn": "w001",
                    "k": {"usn": "p 100 1", "esn": "e001"},
                    "v": {"d1":13, "d2":23, "d3":23}
                }, {
                    "psn": "p1004",
                    "wsn": "w001",
                    "k": {"usn": "p 100 1", "esn": "e001"},
                    "v": {"d1":14, "d2":24, "d3":24}
                }]
            }, {
                "tab": "chunk2",
                "line": "line-one",
                "data": [{
                    "psn": "p1001",
                    "wsn": "w001",
                    "k": {"msn": "msn11", "esn": "e001", "usn": "p 100 1"},
                    "v": {}
                }, {
                    "psn": "p1002",
                    "wsn": "w001",
                    "k": {"msn": "msn22", "esn": "e001", "usn": "p 100 1"},
                    "v": {}
                }]
            }]
    });

    let ret = dourl(&data, "vchunk").await.expect("error_3");
    assert_eq!(ret, json!({"code": 1000, "fail": false, "success": true}));

    let data = json!({
                "tab": "chunk",
                "line": "line-one",
                "data": [{
                    "psn": "p1001",
                    "wsn": "w001",
                    "k": {"usn": "p 100 1", "esn": "e001"},
                    "v": {"d1":41, "d2":41, "d3":41}
                }]
    });

    let ret = dourl(&data, "chunk").await.expect("error_3");
    assert_eq!(ret, json!({"code": 1000, "fail": false, "success": true}));

    let data = json!({
                "tab": "chunk2",
                "line": "line-one",
                "data": [{
                    "psn": "p1001",
                    "wsn": "w001",
                    "k": {"msn": "msn22", "esn": "e001", "usn": "p 100 1"},
                    "v": {}
                }]
    });

    let ret = dourl(&data, "chunk").await.expect("error_3");
    assert_eq!(ret, json!({"code": 1000, "fail": false, "success": true}));

    let data = json!({
        "n": "chunk,chunk2",
        "obj": "*",
        "opt": {"and": [
            {"eq": "psn", "v": "p1001"},
            {"eq": "line", "v": "line-one"},
        ]}
    });
    let mut ret = dourl(&data, "get").await.expect("error_4");
    eq_obj_notime(
        &mut ret,
        json!({
            "code": 1000,
            "success": true,
            "fail": false,
            "data": {
                "chunk": {"data": [
                    {"d1": 11, "d2": 21, "d3": 21, "esn": "e001", "line": "line-one", "psn": "p1001", "usn": "p 100 1", "wsn": "w001", "dtp": 0},
                    {"d1": 41, "d2": 41, "d3": 41, "esn": "e001", "line": "line-one", "psn": "p1001", "usn": "p 100 1", "wsn": "w001", "dtp": 0},
                ]},
                "chunk2": {"data": [
                    {"esn": "e001", "line": "line-one", "msn": "msn11", "psn": "p1001", "usn": "p 100 1", "wsn": "w001", "dtp": 0},
                    {"esn": "e001", "line": "line-one", "msn": "msn22", "psn": "p1001", "usn": "p 100 1", "wsn": "w001", "dtp": 0},
                ]}
            }
        }),
        line!(),
        file!(),
    );
}

#[tokio::test]
async fn c_chunk() {
    let data = json!({
        "n": "chunk,chunk2"
    });
    let ret = dourl(&data, "rmtab").await.expect("error_1");
    assert_eq!(ret, json!({"code": 1000, "fail": false, "success": true}));

    let data = json!({
            "ck": false,
            "v": [{
                "tab": "chunk",
                "data": [{
                    "k": {"usn": "p 100 1", "esn": "e001"},
                    "v": {"d1":11, "d2":21, "d3":21}
                }]
            }, {
                "tab": "chunk2",
                "data": [{
                    "k": {"msn": "msn11", "esn": "e001", "usn": "p 100 1"},
                    "v": {}
                }]
            }]
    });

    let ret = dourl(&data, "vchunk").await.expect("error_3");
    assert_eq!(ret, json!({"code": 1000, "fail": false, "success": true}));

    let data = json!({
        "n": "chunk,chunk2",
        "obj": "*",
    });
    let mut ret = dourl(&data, "get").await.expect("error_4");
    eq_obj_notime(
        &mut ret,
        json!({
            "code": 1000,
            "success": true,
            "fail": false,
            "data": {
                "chunk": {"data": [
                    {"d1": 11, "d2": 21, "d3": 21, "esn": "e001", "usn": "p 100 1", "dtp": 0},
                ]},
                "chunk2": {"data": [
                    {"esn": "e001", "msn": "msn11", "usn": "p 100 1", "dtp": 0},
                ]}
            }
        }),
        line!(),
        file!(),
    );
}
